/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weissenborn.colabtex.client.activities;

import java.util.Map;

import com.weissenborn.colabtex.client.ClientFactory;
import com.weissenborn.colabtex.client.events.PadCreatedEvent;
import com.weissenborn.colabtex.client.events.PadCreatedEventHandler;
import com.weissenborn.colabtex.client.events.PadRemovedEvent;
import com.weissenborn.colabtex.client.events.PadRemovedEventHandler;
import com.weissenborn.colabtex.client.events.UserUpdatedEvent;
import com.weissenborn.colabtex.client.events.UserUpdatedEventHandler;
import com.weissenborn.colabtex.client.service.EtherpadService;
import com.weissenborn.colabtex.client.service.EtherpadServiceAsync;
import com.weissenborn.colabtex.client.view.GroupListView;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

/**
 * The Class GroupListActivity which controls the view of the group list in the sidebar
 */
public class GroupListActivity extends AbstractActivity implements GroupListView.Presenter {

	/** The client factory. */
	private ClientFactory clientFactory;

	/**
	 * Instantiates a new group list activity.
	 *
	 * @param clientFactory the client factory
	 */
	public GroupListActivity(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget, com.google.gwt.event.shared.EventBus)
	 */
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		final GroupListView view = clientFactory.getGroupListView();		
		view.setPresenter(this);

		eventBus.addHandler(PadCreatedEvent.TYPE, new PadCreatedEventHandler() {		
			public void onPadCreated(PadCreatedEvent event) {
				//TODO better
				fillGroup(event.getGroupName());
			}
		});
		
		eventBus.addHandler(PadRemovedEvent.TYPE, new PadRemovedEventHandler() {
			public void onPadRemoved(PadRemovedEvent event) {
				view.removePad(event.getGroupName(), event.getPadName());
			}
		});
		
		eventBus.addHandler(UserUpdatedEvent.TYPE, new UserUpdatedEventHandler() {
			public void onUserUpdated(UserUpdatedEvent event) {
				final GroupListView view = clientFactory.getGroupListView();		
				view.clear();
				if( clientFactory.getUser().getPermissions()!=null&& clientFactory.getUser().getPermissions().keySet()!=null)
					for (String group : clientFactory.getUser().getPermissions().keySet()) {
						view.addGroup(group);
						fillGroup(group);
					}
			}
		});
		
		containerWidget.setWidget(view.asWidget());	
	}

	/**
	 * Fill group with pads from that group
	 *
	 * @param group the groups name
	 */
	private void fillGroup(final String group) {
		final GroupListView view = clientFactory.getGroupListView();		
		view.clearPads(group);
		
		EtherpadServiceAsync service = EtherpadService.Util.getInstance();
		AsyncCallback<Map<String, String>> callback = new AsyncCallback<Map<String,String>>() {	
			public void onSuccess(Map<String, String> result) {
				for (String padName : result.keySet()) {
					view.addPad(group, padName, result.get(padName));
				}
			}
			
			public void onFailure(Throwable caught) {
				Window.alert("No connection to server");
			}
		};
		service.listPads(clientFactory.getUser().getPermissions().get(group),group, callback);
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.GroupListView.Presenter#goTo(com.google.gwt.place.shared.Place)
	 */
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}
}
